import java.util.Scanner;

public class Test4 {
    private static int[] dx = {-1, 0, 0, 1};
    private static int[] dy = {0, -1, 1, 0};
    private static int path;
    private static int ret = -1;
    private static boolean[][] vis;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int m = in.nextInt();
        in.nextLine();
        int[][] matrix = new int[n][m];
        vis = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j] = in.nextInt();
            }
            in.nextLine();
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!vis[i][j]) {
                    vis[i][j] = true;
                    path = 1;
                    dfs(matrix, i, j);
                    vis[i][j] = false;
                }
            }
        }
    }

    private static void dfs(int[][] matrix, int row, int col) {
        // 判断当前位置是否是最低点
        int current = matrix[row][col];
        boolean flg = false;
        for (int i = 0; i < 4; i++) {
            int newRow = row + dx[i];
            int newCol = col + dy[i];
            if (newRow >= 0 && newRow < matrix.length
                    && newCol >= 0 && newCol < matrix[0].length) {
                if (current > matrix[newRow][newCol]) {
                    flg = true;
                    break;
                }
            }
        }
        if (!flg) {
            ret = Math.max(ret, path);
            return;
        }

        for (int i = 0; i < 4; i++) {
            int newRow = row + dx[i];
            int newCol = col + dy[i];
            if (newRow >= 0 && newRow < matrix.length
                    && newCol >= 0 && newCol < matrix[0].length
                    && !vis[newRow][newCol]
                    && matrix[newRow][newCol] < current) {
                vis[newRow][newCol] = true;
                path++;
                dfs(matrix, newRow, newCol);
                vis[newRow][newCol] = false;
                path--;
            }
        }
    }
}
